﻿using System;
using System.Linq;
using System.ServiceModel;
using LearningProject.DataContract;
using LearningProject.Interface;

namespace LearningProject.Client
{
        public class EmployeeClient
        {
                public static void Test()
                {
                        using (var channelFactory = new ChannelFactory<IEmployeesService>("employeeService"))
                        {
                                var proxy = channelFactory.CreateChannel();
                                Console.WriteLine("所有员工列表:");
                                ShowAllEmployee(proxy.GetAll().ToArray());

                                Console.WriteLine("");
                                Console.WriteLine("添加一个新员工(009):");
                                proxy.Create(new Employee
                                {
                                        Id = "009",
                                        Department = "行政部",
                                        Grade = "G9",
                                        Name = "肥猫"
                                });
                                ShowAllEmployee(proxy.GetAll().ToArray());

                                Console.WriteLine("");
                                Console.WriteLine("修改员工(009):");
                                proxy.Update(new Employee
                                {
                                        Id = "009",
                                        Department = "行政部",
                                        Grade = "G9",
                                        Name = "肥猪"
                                });
                                ShowAllEmployee(proxy.GetAll().ToArray());

                                Console.WriteLine("");
                                Console.WriteLine("\n删除员工（009）信息：");
                                proxy.Delete("009");
                                ShowAllEmployee(proxy.GetAll().ToArray());

                                Console.ReadKey();
                        }
                }

                private static void ShowAllEmployee(Employee[] employees)
                {
                        Array.ForEach<Employee>(employees, em => Console.WriteLine(em.ToString()));
                }
        }
}
